package com.example.foodDemo.controller;

import com.example.foodDemo.bean.Food;
import com.example.foodDemo.dao.FoodDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    FoodDao foodDao;

    /*查询全部菜品列表*/
    @RequestMapping("/list")
    @ResponseBody
    public Map foodList() {
        Map<String, Object> map = new HashMap<>();
        List<Food> foodList = foodDao.findAll();

        if (foodList != null) {
            map.put("success", true);
            map.put("message", "查询成功");
            map.put("data", foodList);
        } else {
            map.put("success", false);
            map.put("message", "查询失败");
            map.put("data", null);
        }
        return map;
    }

    /*通过id查询菜品*/
    @RequestMapping("/findone/{id}")
    @ResponseBody
    public Map findFoodById(@PathVariable Integer id) {
        Map<String, Object> map = new HashMap<>();

        //通过id查询到食品
        Optional<Food> find = foodDao.findById(id);

        if (!find.isEmpty()) {
            Food food1 = find.get();
            map.put("success", true);
            map.put("message", "查询成功");
            map.put("data", food1);
        } else {
            map.put("success", false);
            map.put("message", "查询失败");
            map.put("data", null);
        }
        return map;
    }


}
